package com.bestcem.xm.award.grpc.service;

import com.bestcem.bp.grpc.v1.base.FileInfo;
import com.bestcem.xm.award.enums.WinningStatusEnum;
import com.bestcem.xm.award.grpc.service.param.draw.*;
import com.bestcem.xm.award.grpc.v1.services.*;
import com.bestcem.xm.award.service.AwardDrawDisplayService;
import com.bestcem.xm.award.service.dto.FileInfoDTO;
import com.bestcem.xm.award.service.dto.draw.*;
import com.bestcem.xm.common.core.domain.web.PageResultDTO;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.sign.grpc.core.GrpcTemplate;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.GRpcUtil;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 抽奖相关grpc服务
 *
 * @author hang.li <hang.li@idiaoyan.com>
 * @version v1.0
 * @date 2021/6/30 17:41
 */
@GrpcService
public class DrawAwardGrpcService extends DrawServiceGrpc.DrawServiceImplBase {

    @Autowired
    @Qualifier("DrawAwardServiceImpl")
    private AwardDrawDisplayService awardDrawDisplayService;

    @Resource
    private GrpcTemplate grpcTemplate;

    /**
     * 抽奖
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void drawAward(DrawAwardRequest request, StreamObserver<DrawAwardResponse> responseObserver) {
        DrawAwardResponse.Builder builder = DrawAwardResponse.newBuilder();
        // 验签
        ServiceResult serviceResult = grpcTemplate.checkSign(request);
        if (!serviceResult.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        DrawAwardParam param = new DrawAwardParam(request.getId(), request.getDeliverId(), request.getPage(), request.getPhone());
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult<DrawAwardResultDTO> result = awardDrawDisplayService.drawAward(param);
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
            DrawAwardResultDTO resultDTO = result.getData();
            AwardInfo.Builder awardInfo = AwardInfo.newBuilder();
            if (Objects.nonNull(resultDTO.getName())) {
                awardInfo.setName(resultDTO.getName());
            }
            if (Objects.nonNull(resultDTO.getLevel())) {
                awardInfo.setLevelValue(resultDTO.getLevel());
            }
            builder.setInfo(awardInfo);
            if (Objects.nonNull(resultDTO.getCode())) {
                builder.setCode(resultDTO.getCode());
            }
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 保存中奖人信息
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void saveWinnerInfo(SaveWinnerInfoRequest request, StreamObserver<SaveWinnerInfoResponse> responseObserver) {
        SaveWinnerInfoResponse.Builder builder = SaveWinnerInfoResponse.newBuilder();
        // 验签
        ServiceResult serviceResult = grpcTemplate.checkSign(request);
        if (!serviceResult.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        SaveWinnerMessageParam param = requestToSaveWinnerMessageParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<Long> result = awardDrawDisplayService.saveWinnerMessage(param);
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 发送短信验证码
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void sendSmsVerificationCode(SendSmsVerificationCodeRequest request, StreamObserver<SendSmsVerificationCodeResponse> responseObserver) {
        SendSmsVerificationCodeResponse.Builder builder = SendSmsVerificationCodeResponse.newBuilder();

        // 验签
        ServiceResult serviceResult = grpcTemplate.checkSign(request);
        if (!serviceResult.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        GetSmsCodeParam param = requestToGetSmsCodeParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult result = awardDrawDisplayService.getSmsAuthCode(param);
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 验证码校验
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void verifySmsVerificationCode(VerifySmsVerificationCodeRequest request, StreamObserver<VerifySmsVerificationCodeResponse> responseObserver) {
        VerifySmsVerificationCodeResponse.Builder builder = VerifySmsVerificationCodeResponse.newBuilder();

        // 验签
        ServiceResult serviceResult = grpcTemplate.checkSign(request);
        if (!serviceResult.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        VerifySmsCodeParam param = requestToVerifySmsCodeParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult result = awardDrawDisplayService.verifySmsAuthCode(param);
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 获取奖励抽奖详情
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void getAwardDetail(GetAwardDetailRequest request, StreamObserver<GetAwardDetailResponse> responseObserver) {
        GetAwardDetailResponse.Builder builder = GetAwardDetailResponse.newBuilder();

        GetAwardDetailParam param = new GetAwardDetailParam(request.getId(), request.getOrgId(), request.getDeliverId());
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult<GetAwardDetailDTO> result = awardDrawDisplayService.getAwardDetail(param);
        GetAwardDetailDTO detailDTO = result.getData();
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
            builder.setWinCount(detailDTO.getWinCount());
            builder.setFailCount(detailDTO.getFailCount());
            List<AwardDetailDTO> awardDetailDTOList = detailDTO.getAwardDetailDTOList();
            List<AwardDetail> detailList = awardDetailDtoListToAwardDetailList(awardDetailDTOList);
            builder.addAllDetails(detailList);
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 获取中奖名单
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void listWinners(ListWinnersRequest request, StreamObserver<ListWinnersResponse> responseObserver) {
        ListWinnersResponse.Builder builder = ListWinnersResponse.newBuilder();

        GetWinnersParam param = requestToGetWinnersParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult<PageResultDTO<RecordDTO>> result = awardDrawDisplayService.listWinners(param);
        PageResultDTO<RecordDTO> pageResult = result.getData();
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
            builder.setTotal(result.getData().getTotal().intValue());

            List<WinnerDetail> winnerDetails = recordDtoListToWinnerDetailList(pageResult.getData());
            builder.addAllWinnerDetails(winnerDetails);
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 更改中奖状态
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void updateWinnerStatus(UpdateWinnerStatusRequest request, StreamObserver<UpdateWinnerStatusResponse> responseObserver) {
        UpdateWinnerStatusResponse.Builder builder = UpdateWinnerStatusResponse.newBuilder();

        // 验签
        ServiceResult serviceResult = grpcTemplate.checkSign(request);
        if (!serviceResult.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        UpdateStatusParam param = requestToUpdateStatusParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult<Long> result = awardDrawDisplayService.updateWinnerStatus(param);
        if (result.isSuccess()) {
            builder.setCount(result.getData().intValue());
            builder.setStatus(RpcStatusResult.success());
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 导出中奖名单
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void exportWinner(ExportWinnerRequest request, StreamObserver<ExportWinnerResponse> responseObserver) {
        ExportWinnerResponse.Builder builder = ExportWinnerResponse.newBuilder();

        ExportWinnersParam param = requestToExportWinnersParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult<FileInfoDTO> result = awardDrawDisplayService.exportWinners(param);
        FileInfoDTO fileInfoDTO = result.getData();
        if (result.isSuccess()) {
            FileInfo.Builder fileInfo = FileInfo.newBuilder();
            fileInfo.setId(fileInfoDTO.getId())
                    .setName(fileInfoDTO.getName())
                    .setUrl(fileInfoDTO.getUrl());
            builder.setStatus(RpcStatusResult.success());
            builder.setFileInfo(fileInfo);
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 中奖名单中的奖项信息
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void listWinnerDetails(ListWinnerDetailsRequest request, StreamObserver<ListWinnerDetailsResponse> responseObserver) {
        ListWinnerDetailsResponse.Builder builder = ListWinnerDetailsResponse.newBuilder();

        ListWinnerDetailsParam param = requestToListWinnerDetailsParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult<PageResultDTO<RecordDetailDTO>> result = awardDrawDisplayService.listWinnerDetails(param);
        PageResultDTO<RecordDetailDTO> pageResult = result.getData();
        if (result.isSuccess()) {
            builder.setStatus(RpcStatusResult.success());
            builder.setTotal(result.getData().getTotal().intValue());

            List<CodeAwardInfo> codeAwardInfoList = recordDetailDtoListToCodeAwardInfoList(pageResult.getData());
            builder.addAllInfo(codeAwardInfoList);
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 导出中奖名单中的奖项信息
     *
     * @param request          请求
     * @param responseObserver 响应
     */
    @Override
    public void exportWinnerDetails(ExportWinnerDetailsRequest request, StreamObserver<ExportWinnerDetailsResponse> responseObserver) {
        ExportWinnerDetailsResponse.Builder builder = ExportWinnerDetailsResponse.newBuilder();

        ExportWinnerDetailsParam param = requestToExportWinnerDetailsParam(request);
        // 参数校验
        Set<ConstraintViolation<Object>> violations = GRpcUtil.validateParam(param);
        if (!CollectionUtils.isEmpty(violations)) {
            ConstraintViolation<Object> violation = violations.iterator().next();
            builder.setStatus(RpcStatusResult.error(violation.getMessage()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        ServiceResult<FileInfoDTO> result = awardDrawDisplayService.exportWinnerDetails(param);
        FileInfoDTO fileInfoDTO = result.getData();
        if (result.isSuccess()) {
            FileInfo.Builder fileInfo = FileInfo.newBuilder();
            fileInfo.setId(fileInfoDTO.getId())
                    .setName(fileInfoDTO.getName())
                    .setUrl(fileInfoDTO.getUrl());
            builder.setStatus(RpcStatusResult.success());
            builder.setFileInfo(fileInfo);
        } else {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 更改中中奖状态
     *
     * @param request 请求参数
     * @return UpdateStatusParam
     */
    private UpdateStatusParam requestToUpdateStatusParam(UpdateWinnerStatusRequest request) {
        UpdateStatusParam param = new UpdateStatusParam();
        param.setAwardId(request.getId());
        param.setRecordIds(request.getRecordIdsList());
        param.setOrgId(request.getOrgId());
        return param;
    }

    /**
     * 保存信息参数转换
     *
     * @param request 请求参数
     * @return SaveWinnerMessageParam
     */
    private SaveWinnerMessageParam requestToSaveWinnerMessageParam(SaveWinnerInfoRequest request) {
        SaveWinnerMessageParam param = new SaveWinnerMessageParam();
        param.setAwardPage(request.getPage());
        param.setPhone(request.getPhone());
        AddressParam address = new AddressParam();
        address.setProvince(request.getWinner().getAddress().getProvince());
        address.setCity(request.getWinner().getAddress().getCity());
        address.setMore(request.getWinner().getAddress().getMore());
        WinnerParam winner = new WinnerParam();
        winner.setAddress(address);
        winner.setName(request.getWinner().getName());
        winner.setContactPhone(request.getWinner().getPhone());
        param.setWinnerParam(winner);
        return param;
    }

    /**
     * 发送短信验证码参数转换
     *
     * @param request 请求参数
     * @return GetSmsCodeParam
     */
    private GetSmsCodeParam requestToGetSmsCodeParam(SendSmsVerificationCodeRequest request) {
        GetSmsCodeParam param = new GetSmsCodeParam();
        param.setAwardId(request.getId());
        param.setDeliverId(request.getDeliverId());
        param.setPhone(request.getPhone());
        return param;
    }

    /**
     * 验证码校验请求参数转换
     *
     * @param request 请求参数
     * @return VerifySmsCodeParam
     */
    private VerifySmsCodeParam requestToVerifySmsCodeParam(VerifySmsVerificationCodeRequest request) {
        VerifySmsCodeParam param = new VerifySmsCodeParam();
        param.setAwardId(request.getId());
        param.setDeliverId(request.getDeliverId());
        param.setAwardPage(request.getPage());
        param.setCode(request.getCode());
        param.setPhone(request.getPhone());
        param.setSurveyId(request.getSurveyId());
        return param;
    }

    /**
     * 获取中奖名单请求参数转换
     *
     * @param request 请求参数
     * @return GetWinnersParam
     */
    private GetWinnersParam requestToGetWinnersParam(ListWinnersRequest request) {
        GetWinnersParam param = new GetWinnersParam();
        param.setOrgId(request.getOrgId())
                .setAwardId(request.getId())
                .setDeliverId(request.getDeliverId())
                .setRecordId(request.getRecordId())
                .setPhone(request.getPhone())
                .setStatus(request.getStatusValue())
                .setLevel(request.getInfo().getLevelValue())
                .setName(request.getInfo().getName());
        PageParam page = new PageParam();
        page.setSize(request.getPage().getSize());
        page.setPage(request.getPage().getPage());
        param.setPage(page);
        return param;
    }

    /**
     * 中奖名单返回数据封装转换
     *
     * @param dtoList List<RecordDTO>
     * @return List<WinnerDetail>
     */
    private List<WinnerDetail> recordDtoListToWinnerDetailList(List<RecordDTO> dtoList) {
        List<WinnerDetail> winnerDetails = new ArrayList<>();
        for (RecordDTO recordDTO : dtoList) {
            WinnerDetail.Builder winnerDetail = WinnerDetail.newBuilder();
            if (Objects.nonNull(recordDTO.getRecordId())) {
                winnerDetail.setRecordId(recordDTO.getRecordId());
            }
            if (Objects.nonNull(recordDTO.getPhone())) {
                winnerDetail.setPhone(recordDTO.getPhone());
            }
            if (Objects.nonNull(recordDTO.getStatus())) {
                winnerDetail.setStatusValue(recordDTO.getStatus());
            }
            if (Objects.nonNull(recordDTO.getReceiveTime()) && recordDTO.getStatus() != WinningStatusEnum.NO_WIN.getIndex()) {
                winnerDetail.setReceiveTime(DateUtil.getTimestamp(recordDTO.getReceiveTime()));
            }
            if (Objects.nonNull(recordDTO.getCode())) {
                winnerDetail.setCode(recordDTO.getCode());
            }
            if (Objects.nonNull(recordDTO.getSurveySeq())) {
                // winnerDetail.setSurveySeq(recordDTO.getSurveySeq());
            }
            AwardInfo.Builder awardInfo = AwardInfo.newBuilder();
            if (Objects.nonNull(recordDTO.getWinName())) {
                awardInfo.setName(recordDTO.getWinName());
            }
            if (Objects.nonNull(recordDTO.getWinLevel())) {
                awardInfo.setLevelValue(recordDTO.getWinLevel());
            }
            winnerDetail.setInfo(awardInfo);

            Winner.Builder winner = Winner.newBuilder();
            if (Objects.nonNull(recordDTO.getName())) {
                winner.setName(recordDTO.getName());
            }
            if (Objects.nonNull(recordDTO.getAddress())) {
                Address.Builder address = Address.newBuilder();
                if (Objects.nonNull(recordDTO.getAddress().getProvince())) {
                    address.setProvince(recordDTO.getAddress().getProvince());
                }
                if (Objects.nonNull(recordDTO.getAddress().getCity())) {
                    address.setCity(recordDTO.getAddress().getCity());
                }
                if (Objects.nonNull(recordDTO.getAddress().getMore())) {
                    address.setMore(recordDTO.getAddress().getMore());
                }
                winner.setAddress(address);
            }
            winnerDetail.setWinner(winner);
            winnerDetails.add(winnerDetail.build());
        }
        return winnerDetails;
    }

    /**
     * 获取中奖名单奖项信息请求参数转换
     *
     * @param request 请求参数
     * @return ListWinnerDetailsParam
     */
    private ListWinnerDetailsParam requestToListWinnerDetailsParam(ListWinnerDetailsRequest request) {
        ListWinnerDetailsParam param = new ListWinnerDetailsParam();
        param.setAwardId(request.getId())
                .setOrgId(request.getOrgId())
                .setDeliverId(request.getDeliverId())
                .setRecordId(request.getRecordId())
                .setName(request.getInfo().getName())
                .setLevel(request.getInfo().getLevelValue())
                .setStatus(request.getStatusValue());
        PageParam page = new PageParam();
        page.setPage(request.getPage().getPage());
        page.setSize(request.getPage().getSize());
        param.setPage(page);
        return param;
    }

    /**
     * 中奖名单中的奖项信息返回数据转换
     *
     * @param dtoList List<RecordDetailDTO>
     * @return List<CodeAwardInfo>
     */
    private List<CodeAwardInfo> recordDetailDtoListToCodeAwardInfoList(List<RecordDetailDTO> dtoList) {
        List<CodeAwardInfo> codeAwardInfoList = new ArrayList<>();
        for (RecordDetailDTO recordDetailDTO : dtoList) {
            CodeAwardInfo.Builder builder = CodeAwardInfo.newBuilder();
            if (Objects.nonNull(recordDetailDTO.getRecordId())) {
                builder.setRecordId(recordDetailDTO.getRecordId());
            }
            if (Objects.nonNull(recordDetailDTO.getCode())) {
                builder.setCode(recordDetailDTO.getCode());
            }

            AwardInfo.Builder awardInfo = AwardInfo.newBuilder();
            if (Objects.nonNull(recordDetailDTO.getLevel())) {
                awardInfo.setLevelValue(recordDetailDTO.getLevel());
            }
            if (Objects.nonNull(recordDetailDTO.getName())) {
                awardInfo.setName(recordDetailDTO.getName());
            }
            builder.setInfo(awardInfo);
            codeAwardInfoList.add(builder.build());
        }
        return codeAwardInfoList;
    }

    /**
     * 导出中奖名单请求参数转换
     *
     * @param request 请求参数
     * @return ExportWinnersParam
     */
    private ExportWinnersParam requestToExportWinnersParam(ExportWinnerRequest request) {
        ExportWinnersParam param = new ExportWinnersParam();
        param.setAwardId(request.getId())
                .setOrgId(request.getOrgId())
                .setDeliverId(request.getDeliverId());
        return param;
    }

    /**
     * 导出中奖名单中的奖项信息请求参数转换
     *
     * @param request 请求参数
     * @return ExportWinnerDetailsParam
     */
    private ExportWinnerDetailsParam requestToExportWinnerDetailsParam(ExportWinnerDetailsRequest request) {
        ExportWinnerDetailsParam param = new ExportWinnerDetailsParam();
        param.setAwardId(request.getId())
                .setOrgId(request.getOrgId())
                .setDeliverId(request.getDeliverId())
                .setLevel(request.getInfo().getLevelValue())
                .setName(request.getInfo().getName())
                .setStatus(request.getStatusValue());
        return param;
    }

    /**
     * 获取奖励抽奖详情参数转换
     *
     * @param list List<AwardDetailDTO>
     * @return List<AwardDetail>
     */
    private List<AwardDetail> awardDetailDtoListToAwardDetailList(List<AwardDetailDTO> list) {
        List<AwardDetail> detailList = new ArrayList<>();
        for (AwardDetailDTO awardDetailDTO : list) {
            AwardDetail.Builder awardDetail = AwardDetail.newBuilder();
            AwardInfo.Builder awardInfo = AwardInfo.newBuilder();
            if (Objects.nonNull(awardDetailDTO.getName())) {
                awardInfo.setName(awardDetailDTO.getName());
            }
            if (Objects.nonNull(awardDetailDTO.getLevel())) {
                awardInfo.setLevelValue(awardDetailDTO.getLevel());
            }
            awardDetail.setInfo(awardInfo);
            if (Objects.nonNull(awardDetailDTO.getWinCount())) {
                awardDetail.setWinCount(awardDetailDTO.getWinCount());
            }
            if (Objects.nonNull(awardDetailDTO.getAwardCount())) {
                awardDetail.setCount(awardDetailDTO.getAwardCount());
            }
            detailList.add(awardDetail.build());
        }
        return detailList;
    }
}